package com.luhuiguo.cms.server.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.service.AttachmentService;
import com.luhuiguo.cms.shared.rpc.RemoveAttachments;
import com.luhuiguo.cms.shared.rpc.RemoveAttachmentsResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class RemoveAttachmentsHandler implements ActionHandler<RemoveAttachments, RemoveAttachmentsResult> {

    @Autowired
    private AttachmentService attachmentService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public RemoveAttachmentsResult execute(RemoveAttachments action, ExecutionContext context) throws ActionException {

        List<Long> list = action.get();

        for (Long id : list) {
            attachmentService.deleteAttachment(id);
        }

        return new RemoveAttachmentsResult(list.size());

    }

    @Override
    public Class<RemoveAttachments> getActionType() {
        return RemoveAttachments.class;
    }

    @Override
    public void rollback(RemoveAttachments action, RemoveAttachmentsResult result, ExecutionContext context)
            throws ActionException {

    }

}
